package perso.marcr.lst.launcher;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import perso.marcr.util.ConverterByte;
import perso.marcr.util.Unit;

public class CmdLine {

	private static Options options = new Options();	

	public static String directory = ".";
	public static double sizeToCopy = ConverterByte.convert(1, Unit.GO, Unit.OCTET);
	public static boolean bSkipBigScenario = false;
	public static boolean bSkipAverageScenario = false;
	public static boolean bSkipSmallScenario = false;
	public static boolean bSkipVerySmallScenario = false;
	
	public static boolean bKeepTestFiles = false;
	public static boolean bUseCmdLine = false;

	public static String reportLabel = "report";

	private static void initOptions()
	{
		options.addOption("directory", true, "directory to test\nDefault to "+directory);		
		options.addOption("help",false,"print this message");
		options.addOption("sizeToCopy", true, "The size to copy in Go.\nDefault is "+ConverterByte.toHumanReadable(6, Unit.GO));
		addOption("skip_big_scenario",false,"skip big file scenarion tests.",Boolean.toString(bSkipBigScenario));
		addOption("skip_average_scenario",false,"skip average file scenarion tests.",Boolean.toString(bSkipAverageScenario));
		addOption("skip_small_scenario",false,"skip small file scenarion tests.",Boolean.toString(bSkipSmallScenario));
		addOption("skip_verysmall_scenario",false,"skip verysmall file scenarion tests.",Boolean.toString(bSkipVerySmallScenario));		
		addOption("reportLabel",true,"The report label file",reportLabel);
		addOption("keep_test_files",false,"keep file tests.",Boolean.toString(bKeepTestFiles));
		addOption("cmdline",false,"launch in cmdline instead of jetty mode",Boolean.toString(bUseCmdLine));
	}

	private static void addOption(String optionName, boolean hasArg, String description, String defaultValue)
	{
		options.addOption(optionName,hasArg,description+"\nDefault value: "+defaultValue);
	}

	public static boolean load(String[] args)
	{

		initOptions();			

		boolean bReturn = false;

		// create the parser
		CommandLineParser parser = new BasicParser();		

		try {
			// parse the command line arguments
			CommandLine cmd =parser.parse( options, args);
			loadOptions(cmd);
			bReturn = true;

		} catch (ParseException e) {
			// oops, something went wrong
			System.err.println( "Parsing failed.  Reason: " + e.getMessage() );
			usage();
		}

		return bReturn;
	}

	private static void usage()
	{
		HelpFormatter formatter = new HelpFormatter();
		formatter.printHelp( "lst (lan speed test)", options );
		System.exit(0);
	}

	private static void loadOptions(CommandLine cmd)
	{
		if(cmd.hasOption("directory"))
		{
			directory = cmd.getOptionValue("directory");
		}

		if(cmd.hasOption("reportLabel"))
		{
			reportLabel = cmd.getOptionValue("reportLabel");
		}

		if(cmd.hasOption("help"))
		{
			usage();			
		}

		if(cmd.hasOption("sizeToCopy"))
		{
			double argSize = Double.parseDouble(cmd.getOptionValue("sizeToCopy"));
			sizeToCopy = ConverterByte.convert(argSize, Unit.GO, Unit.OCTET);
		}

		bSkipBigScenario = cmd.hasOption("skip_big_scenario");
		bSkipAverageScenario = cmd.hasOption("skip_average_scenario");
		bSkipSmallScenario = cmd.hasOption("skip_small_scenario");
		bSkipVerySmallScenario = cmd.hasOption("skip_verysmall_scenario");
		bKeepTestFiles = cmd.hasOption("keep_test_files");
		bUseCmdLine = cmd.hasOption("cmdline");
	}


}
